@echo off
chcp 65001 >nul
echo =========================================
echo    GMCM 2025 单个算例测试
echo =========================================
echo 说明: 测试修改后的代码是否正常工作
echo.

REM 完整重新编译
echo === 完整重新编译 ===
echo [1/4] 清理旧文件...
if exist "competition_scheduler.exe" del "competition_scheduler.exe"
if exist "main_competition.o" del "main_competition.o"
if exist "graph_core.o" del "graph_core.o"
if exist "csv_loader.o" del "csv_loader.o" 
if exist "optimizer.o" del "optimizer.o"

echo [2/4] 编译源文件...
gcc -std=c99 -O2 -Wall -Wextra -c main_competition.c -o main_competition.o
if %ERRORLEVEL% neq 0 ( echo ❌ main_competition.c 编译失败! && pause && exit /b 1 )

gcc -std=c99 -O2 -Wall -Wextra -c graph_core.c -o graph_core.o
if %ERRORLEVEL% neq 0 ( echo ❌ graph_core.c 编译失败! && pause && exit /b 1 )

gcc -std=c99 -O2 -Wall -Wextra -c csv_loader.c -o csv_loader.o
if %ERRORLEVEL% neq 0 ( echo ❌ csv_loader.c 编译失败! && pause && exit /b 1 )

gcc -std=c99 -O2 -Wall -Wextra -c optimizer.c -o optimizer.o
if %ERRORLEVEL% neq 0 ( echo ❌ optimizer.c 编译失败! && pause && exit /b 1 )

echo [3/4] 链接可执行文件...
gcc -o competition_scheduler main_competition.o graph_core.o csv_loader.o optimizer.o -lm
if %ERRORLEVEL% neq 0 ( echo ❌ 链接失败! && pause && exit /b 1 )

echo [4/4] 验证可执行文件...
if not exist "competition_scheduler.exe" (
    echo ❌ 可执行文件未生成!
    pause
    exit /b 1
)
echo ✅ 编译完成!
echo.

REM 测试单个算例 (Conv_Case1)
echo === 测试运行 Conv_Case1 ===
set test_case=Conv_Case1
set nodes_file=data\CSV-version\%test_case%_Nodes.csv
set edges_file=data\CSV-version\%test_case%_Edges.csv
set output_dir=result-all\%test_case%

echo 测试参数:
echo - 节点文件: %nodes_file%
echo - 边文件: %edges_file%
echo - 输出目录: %output_dir%
echo.

REM 检查输入文件
if not exist "%nodes_file%" (
    echo ❌ 节点文件不存在: %nodes_file%
    pause
    exit /b 1
)
if not exist "%edges_file%" (
    echo ❌ 边文件不存在: %edges_file%
    pause
    exit /b 1
)

REM 确保输出目录存在
if not exist "%output_dir%" mkdir "%output_dir%"

echo 开始运行测试...
echo.
.\competition_scheduler.exe "%nodes_file%" "%edges_file%" "%test_case%" "%output_dir%"

if %ERRORLEVEL% equ 0 (
    echo.
    echo ✅ 测试成功完成！
    
    REM 日志文件现在直接写到正确位置
    
    echo.
    echo === 生成的文件 ===
    if exist "%output_dir%\%test_case%_schedule.txt" (
        echo ✅ 调度文件: %output_dir%\%test_case%_schedule.txt
    )
    if exist "%output_dir%\%test_case%_summary.txt" (
        echo ✅ 汇总文件: %output_dir%\%test_case%_summary.txt
        echo.
        echo === 汇总内容 ===
        type "%output_dir%\%test_case%_summary.txt"
    )
    if exist "%output_dir%\iteration_convergence.csv" (
        echo ✅ 收敛日志: %output_dir%\iteration_convergence.csv
    )
    
) else (
    echo ❌ 测试失败 (错误代码: %ERRORLEVEL%)
)

REM 无需清理

echo.
echo === 测试完成 ===
pause
